<?php
namespace Home\Controller;
use Common\Controller\HomebaseController;
use Home\Service\AccountService;
// +----------------------------------------------------------------------
// | Date:2017年3月21日
// +----------------------------------------------------------------------
// | Author: EK_熊<1439527494@qq.com>
// +----------------------------------------------------------------------
// | Description: 此文件作用于****
// +----------------------------------------------------------------------
Class TeamController extends HomebaseController{
    
    public function _initialize(){
        $this->model = D('AttendanceDay');
    }
    
    
    public function index(){
        $lastday_end = date('Y-m-d', strtotime("-1 day"));
        
        $month_cur_start = date('Y-m-01', strtotime(date("Y-m-d")));//本月开始时间，1号
        $month_cur_end = date('Y-m-d', strtotime("-1 day"));//本月的结束时间，即昨天
        
        $month_pre_start = date("Y-m-d",mktime(0,0,0,date("m")-1,1,date("Y")));//上一个月开始
        $month_pre_end =date("Y-m-d",mktime(23,59,59,date("m"),0,date("Y")));//上一个月结束时间        
        
        
        //获取下属
        $account = new AccountService();
        $account_map['leader_id']=$this->user_uid;
      //  $account_map['_string']="`status`=1 or (`status`=0 and `quit_date`>$month_pre_start)";
        $account_map['status']=1;
        $underlingUserInfo = $account->get_all_user_baseinfo('sn,ep_name',$account_map);
        $underling = array_keys($underlingUserInfo);
        $underling_name = array_values($underlingUserInfo);
        
        $underling[] = $this->user_sn;
        $map['sn'] = ['in',$underling];
        $map['date'] = ['between',["$month_pre_start 00:00:00","$month_pre_end 23:59:59"]];
        
        $total_work = work_days_in_interval($month_cur_start,$lastday_end);//本月时间范围：本月1号开始至昨天
        $total_work_pre = work_days_in_interval($month_pre_start,$month_pre_end);//上一个月的时间范围：1号到最后一天
        
        $leaveType = array_values(C('VACATION_TYPE_EN'));
        $leaveType[] = 'leave_evection';
        $leaveType[] = 'leave_long_distance';
        foreach ($leaveType as $leave_val){
            $leaveTotalStr .= "- sum($leave_val)";
            $leaveTypeStr .="sum($leave_val) as $leave_val,";
        }
        $beaseFiels = "sn,name,
            sum(work_effect) as work_effect,
            sum(work_actual) as work_actual,
            sum(overtime) as overtime,".$leaveTypeStr;
        
        $field_last = $beaseFiels."({$total_work_pre['min']}"."{$leaveTotalStr} - sum(work_actual)) as stu_absent";
        $field_cur = $beaseFiels."({$total_work['min']}"."{$leaveTotalStr} - sum(work_actual)) as stu_absent";
        $last_mon = $this->model->where($map)->group('name')->order('work_actual DESC')->getField($field_last);
        $map['date'] = ['between',["$month_cur_start 00:00:00","$month_cur_end 23:59:59"]];
        $cur_mon = $this->model->where($map)->group('name')->order('work_actual DESC')->getField($field_cur);
       
        if (count($cur_mon) !== count($underlingUserInfo)) {
            foreach ($underlingUserInfo as $k=>$v){
               if (!$cur_mon[$k]) {
                   $cur_mon[$k]['name'] = $v;
               }
            }
        }
        
        $this->assign('last_mon',$last_mon);
        $this->assign('cur_mon',$cur_mon);
        $this->display();       
    }
    
    
}